package ru.vba.utils;

import java.net.UnknownHostException;

import ru.vba.system.Substance;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class MongoUtils{
	private static Mongo mongo;
	{
		try {
			mongo = new Mongo("localhost", 27017);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (MongoException e) {
			e.printStackTrace();
		}
	}
	public static <T extends Substance> void save(T substance){
		Datastore store = getDatastore(substance.getClass());
		store.save(substance);
	}
	public static <T extends Substance> Datastore getDatastore(Class<T> mapper) throws  MongoException{
		Morphia m = new Morphia();

		m.map(mapper);
		return m.createDatastore(mongo, mapper.getCanonicalName());
	}
}